var WxParse = require('../../wxParse/wxParse.js');
var app = getApp()
Page({
  data: {
    did:32,
    list: [],
    relate:[],
    error:false,
    status:true,
    error_color:false,
    hidden:false,
    title:'',
    error_id:'',
    subject:'',
    subject_id:'',
    type_name:'',
    articleContent0:false,
    articleContent1:false,
    articleContent2:false,
    articleContent3:false,
    articleContent4:false,
    articleContent5:false,
    articleContent6:false,
    articleContent7:false,
    articleContent8:false,
    articleContent9:false,
    explainContent0:false,
    explainContent1:false,
    explainContent2:false,
    explainContent3:false,
    explainContent4:false,
    explainContent5:false,
    explainContent6:false,
    explainContent7:false,
    explainContent8:false,
    explainContent9:false
  },
  onLoad: function(options) {
    var that = this;
    wx.setNavigationBarTitle({
      title:options.title
    });
    var subject = '';
    if( options.subject_id == 2 || options.subject_id == 14  ) subject = '数学';
    if( options.subject_id == 4 || options.subject_id == 16  ) subject = '物理';
    that.setData({ title:options.title,subject:subject,type_name:options.type,subject_id:options.subject_id,did:options.did });
    wx.request({
        url: app.globalData.domain+'/wx/index.php',
        method:'POST',
        data: {did: options.did,'type': 'detail' },
        header: {'Content-Type': 'application/x-www-form-urlencoded'},
        success: function(res) {
          var con_length = res.data.data.content.length;
          for (var i = 0, len = con_length; i < len; ++i) {
             var _articleContent = 'articleContent'+i;
             var _explainContent = 'explainContent'+i;
           
              if(res.data.data.content[i].articleContent) {
                res.data.data.content[i].articleContent = res.data.data.content[i].articleContent.replace(/&#39;/g,"<sup>'</sup>");
                that.setData({ _articleContent:true });
                WxParse.wxParse(_articleContent, 'html', res.data.data.content[i].articleContent, that,5);
                res.data.data.content[i]['articleContent'] = 'articleContent'+i;
              }

              if(res.data.data.content[i].explainContent){
                res.data.data.content[i].explainContent = res.data.data.content[i].explainContent.replace(/&#39;/,"<sup>'</sup>");
                that.setData({ _explainContent:true });
                WxParse.wxParse(_explainContent, 'html', res.data.data.content[i].explainContent, that,5);
                res.data.data.content[i]['explainContent'] = 'explainContent'+i;
              }
          } 
          that.setData({ list:res.data.data,relate:res.data.related,hidden: !that.data.hidden });
        }
    });

  },
   
 widgetsToggle: function (e) {
    var id = e.currentTarget.id, list = this.data.list;
    for (var i = 0, len = list.length; i < len; ++i) {
      if (list[i].id == id) {
        list[i].open = !list[i].open;
      } else {
        list[i].open = false;
      }
    }
   this.setData({
      list: list
    }); 
 },
  showError: function(e) {
    var that = this;
    var error = that.data.error;
    var name = app.globalData.userInfo.nickName;
    if( !that.data.error_id ){
        wx.request({
            url: app.globalData.domain+'/wx/index.php',
            method:'POST',
            data: {'openid': '','name':name,'content':'','position':that.data.title,'type': 'suggest'},
            header: {'Content-Type': 'application/x-www-form-urlencoded'},
            success: function(res) {
                if( !res.data.error ) that.setData({ error_id:res.data.data }); 
            }
        });
    }
    error = !error
    this.setData({
        error: error,error_color:error
    });
  },
  saveError:function(e){
    var that = this;
    var name = app.globalData.userInfo.nickName;
    var content = e.currentTarget.id;
    var _error_id = that.data.error_id;
    if( !_error_id ){
        wx.request({//封装request方法
            url: app.globalData.domain+'/wx/index.php',
            method:'POST',
            data: {'openid': '','name':name,'content':content,'position':that.data.title,'type': 'suggest'},
            header: {'Content-Type': 'application/x-www-form-urlencoded'},
            success: function(res) {
                if( !res.data.error ) that.setData({ error_id:res.data.data }); 
            }
        });
    }else{
      wx.request({
          url: app.globalData.domain+'/wx/index.php',
          method:'POST',
          data: {'error_id':_error_id,'content':content,'type': 'editSuggest'},
          header: {'Content-Type': 'application/x-www-form-urlencoded'},
          success: function(res) {
              that.setData({status:false});
              setTimeout(function () {
                    that.setData({status:true,error_color:false,error:false});
                }, 2000); 
          }
      });
    }
  },
  jumpLink:function(e){
    var _id = e.currentTarget.id,_title=e.currentTarget.dataset.title;
    wx.redirectTo({
      url: '../others/others?did='+_id+'&title='+_title+'&type='+this.data.type_name+'&subject_id'+this.data.subject_id
    })
  },
  shoSuggest:function(){
    wx.navigateTo({
      url: '../suggest/suggest'
    })
  },
  onShareAppMessage: function () {
    return {
      title: this.data.type_name,
      desc: app.globalData.userInfo.nickName+'分享给你'+this.data.subject+this.data.title+'公式【海风教育】',
      path: 'page/others/others?did='+ this.data.did+'&title='+ this.data.title+'&type='+this.data.type_name+'&subject_id'+this.data.subject_id
    }
  }
});